package com.kaishengit.service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.transaction.annotation.Transactional;





import com.kaishengit.dao.ContactDao;
import com.kaishengit.dao.DealContactDao;
import com.kaishengit.dao.DealDao;
import com.kaishengit.dao.NoteDao;
import com.kaishengit.dao.core.Page;
import com.kaishengit.pojo.Contact;
import com.kaishengit.pojo.Deal;
import com.kaishengit.pojo.DealContact;
import com.kaishengit.pojo.Note;
import com.kaishengit.pojo.User;
import com.kaishengit.util.DataUtil;

@Transactional
public class DealService {
	private DealDao dealDao;
	private ContactDao contactDao;
	private DealContactDao dealcontactDao;
	private NoteDao noteDao;
	
	
	
	public void save(User user, Deal deal, String contactid) {
		System.out.println(contactDao==null);
		Contact contact = contactDao.findByUnique("id",contactid);
		System.out.println(contact.getName());
		DealContact dc = new DealContact();
		dc.setContact(contact);
		dc.setDeal(deal);
		dc.setRole("mainmember");
		Set<DealContact> dcSet = new HashSet<DealContact>();
		dcSet.add(dc);
		deal.setDcSet(dcSet);
		deal.setProgress("pend");
		deal.setUser(user);
		deal.setContactname(contact.getName());
		Note note = new Note();
		String createtime = DataUtil.getNow();
		note.setCreatetime(createtime);
		note.setContent("这个交易："+deal.getName()+"被创建在在"+createtime);
		note.setViews("all");
		note.setDeal(deal);
		note.setType("create");
		dealDao.save(deal);
		dealcontactDao.save(dc);
		
		
	}
	public void fingAllUser() {
		// TODO Auto-generated method stub
		
	}
	public Page<Deal> findAllPendingDeal(User user, String progress,Page<Deal> page) {
		return dealDao.findalldeal("user",user,"progress",progress,page);
		
	}
	public Deal findDealById(String id) {
		return dealDao.findByUnique("id",id);
		
	}
	public Note update(String id, String progress) {
		Deal deal = dealDao.findByUnique("id", id);
		deal.setProgress(progress);
		//这里是将这个交易状态的改变的note存进t_note表中
		Note note = new Note();
		String createtime = DataUtil.getNow();
		note.setCreatetime(createtime);
		note.setContent("这个交易："+deal.getName()+"    被修改为"+progress+"在"+createtime);
		note.setViews("all");
		note.setDeal(deal);
		note.setType(progress);
		noteDao.save(note);
		return note;	
	}
	public void delDeal(String id) {
		Deal deal = dealDao.findByUnique("id",id);
		List<Note> listnote = noteDao.findAllByProperty("deal",deal);
		if(listnote != null){
			for(Note note:listnote){
				noteDao.del(note.getId());
			}
		}
		List<DealContact> list = dealcontactDao.findAllByProperty("deal",deal);
		if(list != null){
			for(DealContact n:list){
				dealcontactDao.del(n.getId());
			}
		}
		dealDao.del(id);
	}
	
	//set
	public void setDealDao(DealDao dealDao) {
		this.dealDao = dealDao;
	}
	public void setContactDao(ContactDao contactDao) {
		this.contactDao = contactDao;
	}
	public void setNoteDao(NoteDao noteDao) {
		this.noteDao = noteDao;
	}
	public void setDealcontactDao(DealContactDao dealcontactDao) {
		this.dealcontactDao = dealcontactDao;
	}
	public void updateDeal(User user, Deal deal, String contactid) {
		deal.setUser(user);
		Contact contact = contactDao.findByUnique("id",contactid);
		String hql="from DealContact where role=:role and deal=:deal";
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("role","mainmember");
		map.put("deal",deal);
		DealContact dc = dealcontactDao.findUnique(hql,map);
		dc.setContact(contact);
		dealDao.save(deal);
		
	}
	


	
	
}
